package 力扣;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yyq
 * @create 2022-04-11 20:04
 */
public class leetcode113 {
    public static void main(String[] args) {
        TreeNode root= new TreeNode(1);
        root.left=new TreeNode(2);
        root.right=new TreeNode(3);
        List<List<Integer>> lists = new ArrayList<>();
        pathSum(root,3);

    }
    public static List<List<Integer>> pathSum(TreeNode root, int targetSum) {

        List<List<Integer>> lists = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        preOrder(list,lists,root,targetSum);
        return lists;
    }

    private static void preOrder(List<Integer> list, List<List<Integer>> lists, TreeNode root, int targetSum) {
        list.add(root.val);
        if(root.left==null&&root.right==null){
            int sum=0;
            for (int i = 0; i < list.size(); i++) {
                sum=sum+list.get(i);
            }
            if(sum==targetSum){
                lists.add(new ArrayList<>(list));
            }
        }

        if(root.left!=null) preOrder(list,lists,root.left,targetSum);
        if(root.right!=null) preOrder(list,lists,root.right,targetSum);
        list.remove(list.size()-1);
    }
}
